package com.fandou.speech;

import android.content.Context;

import com.baidu.aip.asrwakeup3.core.recog.listener.MessageStatusRecogListener;
import com.fandou.speech.asr.IRecognizer;
import com.fandou.speech.asr.baidu.OfflineRecognizer;
import com.fandou.speech.tts.ISpeaker;
import com.fandou.speech.tts.ISpeakerListener;
import com.fandou.speech.tts.baidu.Speaker;

public class Speech implements IRecognizer, ISpeaker {

    private volatile Boolean isInited = false;//避免重复初始化

    private IRecognizer recognizer;
    private ISpeaker speaker;

    public boolean init(Context context) {
        synchronized (this) {
            if (isInited) {
                return false;
            }

            initialAsr(context.getApplicationContext());
            initialTts(context.getApplicationContext());

            isInited = true;
        }

        return true;
    }

    public void release() {
        releaseAsr();
        releaseTts();
        isInited = false;
    }

    //Tts /////////////////////////////////////////////

    @Override
    public void initialTts(Context context) {
        speaker = new Speaker();
        speaker.initialTts(context);
    }

    @Override
    public void releaseTts() {
        if (speaker != null) {
            speaker.releaseTts();
            speaker = null;
        }
    }

    @Override
    public long startTts(String text) {
        if (!isInited) {
            return -1;
        }
        return speaker.startTts(text);
    }

    @Override
    public long startTts(String text, ISpeakerListener listener) {
        if (!isInited) {
            return -1;
        }
        return speaker.startTts(text, listener);
    }

    @Override
    public void resumeTts() {
        if (!isInited) {
            return;
        }
        speaker.resumeTts();
    }

    @Override
    public void pauseTts() {
        if (!isInited) {
            return;
        }
        speaker.pauseTts();
    }

    @Override
    public void stopTts() {
        if (!isInited) {
            return;
        }
        speaker.stopTts();
    }

    @Override
    public void stopTts(long speakerId) {
        if (!isInited) {
            return;
        }
        speaker.stopTts(speakerId);
    }

    //Asr /////////////////////////////////////////////

    @Override
    public void initialAsr(Context context) {

        recognizer = new OfflineRecognizer(context);
        recognizer.initialAsr(context);
    }

    @Override
    public void releaseAsr() {
        if (recognizer != null) {
            recognizer.releaseAsr();
            recognizer = null;
        }
    }

    @Override
    public void startAsrCN(String outFile, MessageStatusRecogListener.OnAsrListener onAsrListener) {
        if (!isInited) {
            return;
        }
        recognizer.startAsrCN(outFile, onAsrListener);
    }

    @Override
    public void startAsrEN(String outFile, MessageStatusRecogListener.OnAsrListener onAsrListener) {
        if (!isInited) {
            return;
        }
        recognizer.startAsrEN(outFile, onAsrListener);
    }

    @Override
    public void stopAsr() {
        if (!isInited) {
            return;
        }
        recognizer.stopAsr();
    }


    /////////////////////////////////////////////////////////////////////

    private final static class Singleton {
        private final static Speech instance = new Speech();
    }

    public static Speech getInstance() {
        return Singleton.instance;
    }
}
